<?php
$this->layout = '//layouts/main';
$this->breadcrumbs=array(
	'Pages',
);
$cs = Yii::app()->clientScript;
$cs->registerCoreScript('jquery.ui');
$cs->registerCoreScript('bbq');
$cs->registerCoreScript('cookie');
$cs->registerScriptFile(Yii::app()->themeManager->baseUrl.'/Admin/scripts/jquery.ui.nestedSortable.js');
//$cs->registerCssFile(Yii::app()->theme->baseUrl.'/styles/reset.css');
$cs->registerCssFile(Yii::app()->themeManager->baseUrl.'/new_admin/styles/form.css');
//$cs->registerCssFile(Yii::app()->theme->baseUrl.'/styles/general.css');
$cs->registerCssFile(Yii::app()->themeManager->baseUrl.'/new_admin/styles/widget-box.css');
$cs->registerCssFile(Yii::app()->themeManager->baseUrl.'/new_admin/styles/manage-page.css');
$script = "
var timer = null;
$('#list-container').nestedSortable({
    disableNesting: 'no-nest',
    forcePlaceholderSize: true,
    handle: '.sort-handle',
    items: '.sortable',
    opacity: .6,
    placeholder: 'placeholder',
    tabSize: 25,
    tolerance: 'pointer',
    toleranceElement: '> a',
    'listType' : 'ul',
    start: function(event, ui){
        //ui.item.css('backgroundImage', 'none');
    },
    stop: function(event, ui){
        //jQuery.updateLine();
    },
    update: function(event, ui){
        jQuery('#list-container').nestedSortable('disable');
        var serialized = jQuery('#list-container').nestedSortable('serialize');
        jQuery('.ordering-updated').remove();
        if (timer) {
            clearTimeout(timer);
            timer = null;
        }
        jQuery.get('".$this->createUrl('/Core/service/ajax', array('SID'=>'Cms.page.sort'))."', serialized, function(res){
            jQuery('#list-container').nestedSortable('enable');
            res = eval(res);
            if (res.errors != undefined && res.errors.length <= 0) {
                var message = jQuery('<div></div');
                message.addClass('ordering-updated')
                    .text('The display order of your pages has been updated successfully.');
                jQuery('#list-container').before(message);
                timer = setTimeout(function(){jQuery('.ordering-updated').remove()}, 5000);
            } else {
                var message = 'Error';
                if (jQuery.isArray(res.errors.ErrorCode))
                    message = res.errors.ErrorCode.join('. ');
                alert(message);
            }
        });
    }
});
";
$cs->registerScript(__CLASS__.'#InitNestedSortable', $script);

$script = "
$('#box-content').delegate('#list-container a', 'click', function(){
    var id = $(this).parent().attr('id').replace('items-', '');
    $.cookie('current_page_id', id);
    $('#box-content .main-content').load($(this).attr('href'), function(){
        $('#AjaxLoading').hide();
    });
    return false;
})
$('#box-wrapper').css({'height':'auto'});
$('#box-content').delegate('.sidebar .button, .functions .add-child', 'click', function(){
    $('#box-content .main-content').load($(this).attr('href'), function(){
        $('#AjaxLoading').hide();
    });
    return false;
});
$.current_page_id = $.cookie('current_page_id');
var current_page = $('#box-content .sidebar .button');
if ($.current_page_id != null)
{
    var id = '#items-'+$.current_page_id;
    if ($(id).size())
        current_page = $(id).find('a');
}
current_page.trigger('click');
";
$cs->registerScript(__CLASS__.'#LoadForm', $script);

$script = "
jQuery('.sort-handle .expand, .sort-handle .collapse').click(function(){
    if (jQuery(this).hasClass('expand'))
    {
        jQuery(this).removeClass('expand')
            .closest('.sort-handle').next('ul').show();
    }
    else
    {
        jQuery(this).addClass('expand')
            .closest('.sort-handle').next('ul').hide();
    }
    return false;
});
";
//$cs->registerScript(__CLASS__.'#Toggle', $script);
?>
<div id="box-wrapper">    
    <div class="inner">
        <div id="box-content">                    
            <div class="sidebar">
                <div class="section">
                    <?php echo CHtml::link('<span>Add Page</span>', array('/Cms/admin/page/create'), array('title'=>'Add Page', 'class'=>'button'));?>
                </div>
                <div class="section">
                    <ul id="list-container" class="left-nav">
                        <?php $this->renderNestedPage($models);?>
                    </ul>
                </div>
                <div class="section">
                    <p class="description">Tip: Drag pages up/down to reoder and left/right to create subpages.</p>
                </div>
            </div>
            <div class="main-content">
                
            </div>
        </div>
    </div>
</div>